package com.photologics.logic;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.photologics.main.MainActivity;
import com.photologics.main.R;

public class SelectLevelActivity extends ListActivity {
	public String selectLevel_LogicName;
	byte[][] selectLevel_LogicValue;
	static final String[] logic_level = new String[] { "2X2", "4X4", "6X6" };
	public static String s;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);

		String path = null;
		Intent intent = getIntent();

		path = intent.getStringExtra("filepath");
		Bitmap selectedImage = BitmapFactory.decodeFile(path);

		ImageProcessing.getImage(selectedImage);

		Log.i("level", "OK");
		setListAdapter(new SelectLevel_Adapter(this, logic_level));

	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		s = (String) getListAdapter().getItem(position);
		showDialog(1);

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 1:
			final Dialog logicname_make_Dialog = new Dialog(this,
					R.style.DialogTheme);
			View view1 = getLayoutInflater().inflate(
					R.layout.logicname_make_dialog, null);
			logicname_make_Dialog.setContentView(view1);
			logicname_make_Dialog.setCanceledOnTouchOutside(true);

			ImageButton makename_ok;
			ImageButton makename_cancle;

			makename_ok = (ImageButton) view1
					.findViewById(R.id.button_makename_ok);
			makename_ok.setOnClickListener(new OnClickListener() {
				@SuppressWarnings("deprecation")
				@Override
				public void onClick(View v) {
					EditText editText = (EditText) logicname_make_Dialog
							.findViewById(R.id.EditText_namemake);
					editText.setSingleLine();
					int maxLength = 5;
					InputFilter[] filterArray = new InputFilter[1];
					filterArray[0] = new InputFilter.LengthFilter(maxLength);
					editText.setFilters(filterArray);
					// editText.setFilters(new InputFilter[] { new
					// InputFilter.LengthFilter(5)});

					selectLevel_LogicName = editText.getText().toString();
					Log.i("ssss", s);

					if (s.equals("2X2")) {
						selectLevel_LogicValue = ImageProcessing.makeArray(
								LogicHelper.toLogic, 50);
					} else if (s.equals("4X4")) {
						selectLevel_LogicValue = ImageProcessing.makeArray(
								LogicHelper.toLogic, 100);
					} else if (s.equals("6X6")) {
						selectLevel_LogicValue = ImageProcessing.makeArray(
								LogicHelper.toLogic, 150);
					}

					long samename = LogicHelper.db.createLogic(
							selectLevel_LogicName, selectLevel_LogicValue);

					if (samename == -1) {
						showDialog(2);
					} else {
						Intent intent = new Intent(SelectLevelActivity.this,
								SelectSectionActivity.class);
						intent.putExtra(LogicHelper.LOGIC_NAME_PARAMETER,
								selectLevel_LogicName);
						startActivity(intent);

					}
				}
			});

			makename_cancle = (ImageButton) view1
					.findViewById(R.id.button_makename_cancle);
			makename_cancle.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					logicname_make_Dialog.cancel();
				}
			});

			return logicname_make_Dialog;

		case 2:
			final Dialog samename_dialog = new Dialog(this, R.style.DialogTheme);
			View view2 = getLayoutInflater().inflate(R.layout.samename_dialog,
					null);
			samename_dialog.setContentView(view2);
			samename_dialog.setCanceledOnTouchOutside(true);

			ImageButton samename_ok;

			samename_ok = (ImageButton) view2
					.findViewById(R.id.button_samename_ok);
			samename_ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					samename_dialog.dismiss();
				}
			});

			return samename_dialog;

		}

		return null;

	}
	protected void onPause() {
		LogicHelper.backgroundMusic.onPause(getApplicationContext());
		super.onPause();
	}

	@Override
	protected void onResume() {
		LogicHelper.backgroundMusic.onStart();
		super.onResume();
	}
	@Override
	public void onBackPressed() {
		Intent it = new Intent(SelectLevelActivity.this, MainActivity.class);
		it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(it);
		finish();
	}

}